{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-15T15:58:53.854447Z",
     "start_time": "2020-06-15T15:58:53.838344Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([9.35762297e-14, 8.53304763e-17, 1.00000000e+00, 2.31952283e-16])"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "\n",
    "def softmax(x):\n",
    "    # x 为数组\n",
    "    exp_x = np.exp(x)\n",
    "    sum_exp_x = np.sum(exp_x)\n",
    "    sm_x = exp_x / sum_exp_x\n",
    "    return sm_x\n",
    "\n",
    "\n",
    "x = np.array([10, 3, 40, 4])\n",
    "softmax(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-15T15:59:49.389634Z",
     "start_time": "2020-06-15T15:59:49.386862Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0.  0. nan  0.]\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/yangbin7/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:6: RuntimeWarning: overflow encountered in exp\n",
      "  \n",
      "/home/yangbin7/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:8: RuntimeWarning: invalid value encountered in true_divide\n",
      "  \n"
     ]
    }
   ],
   "source": [
    "x = np.array([10, 2, 10000, 4])\n",
    "print(softmax(x))\n",
    "\n",
    "# 计算 e^x 时,产生了溢出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-15T16:00:30.550514Z",
     "start_time": "2020-06-15T16:00:30.547354Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0. 0. 1. 0.]\n"
     ]
    }
   ],
   "source": [
    "def softmax(x):\n",
    "    max_x = np.max(x)\n",
    "    exp_x = np.exp(x - max_x)  # 减去最大值,防止溢出\n",
    "    sum_exp_x = np.sum(exp_x)\n",
    "    sm_x = exp_x / sum_exp_x\n",
    "    return sm_x\n",
    "\n",
    "\n",
    "x = np.array([10, 2, 10000, 4])\n",
    "print(softmax(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Log Softmax"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-15T16:01:34.892356Z",
     "start_time": "2020-06-15T16:01:34.879366Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/yangbin7/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:2: RuntimeWarning: divide by zero encountered in log\n",
      "  \n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([-inf, -inf,   0., -inf])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.array([10, 2, 10000, 4])\n",
    "np.log(softmax(x))\n",
    "\n",
    "# 计算 log(0) 时出现了下溢出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-15T16:03:47.478337Z",
     "start_time": "2020-06-15T16:03:47.474388Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0. 0. 1. 0.]\n"
     ]
    }
   ],
   "source": [
    "def logsoftmax(x, recover_probs=True):\n",
    "    # LogSoftMax Implementation\n",
    "    max_x = np.max(x)\n",
    "    exp_x = np.exp(x - max_x)\n",
    "    sum_exp_x = np.sum(exp_x)\n",
    "    log_sum_exp_x = np.log(sum_exp_x)\n",
    "    max_plus_log_sum_exp_x = max_x + log_sum_exp_x\n",
    "    log_probs = x - max_plus_log_sum_exp_x\n",
    "\n",
    "    # Recover probs\n",
    "    if recover_probs:\n",
    "        exp_log_probs = np.exp(log_probs)\n",
    "        sum_log_probs = np.sum(exp_log_probs)\n",
    "        probs = exp_log_probs / sum_log_probs\n",
    "        return probs\n",
    "\n",
    "    return log_probs\n",
    "\n",
    "\n",
    "x = np.array([10, 2, 10000, 4])\n",
    "print(logsoftmax(x, recover_probs=True))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Softmax Temperature"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-15T16:05:41.922253Z",
     "start_time": "2020-06-15T16:05:41.918547Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.09, 0.  , 0.67, 0.24])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.array([38, 20, 40, 39])\n",
    "softmax(x).round(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-15T16:05:44.302372Z",
     "start_time": "2020-06-15T16:05:44.299613Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0., 0., 1., 0.])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "softmax(x / 0.001)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-15T16:06:06.881983Z",
     "start_time": "2020-06-15T16:06:06.879253Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.26, 0.22, 0.26, 0.26])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "softmax(x / 100).round(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
